package com.simons.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import lombok.extern.log4j.Log4j2;

@Log4j2
public class IpUtils {
	
	private IpUtils(){
		//do nothing
	}

	/**
	 * @Title: getClientIp 
	 * @Description: 获取客户端真实ip
	 * @param request
	 * @return
	 * @return: String
	 */
	public static String getClientIp(HttpServletRequest request) {

		String ip = new String();
        StringBuffer sb = new StringBuffer();
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-Ip");
            sb.append("X-Real-Ip = {}" + ip);
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
            sb.append("X-Forwarded-For = {}"+ ip);
        }
        
        if(StringUtils.isNotBlank(ip)) {
            ip = ip.split(",")[0];
            sb.append("ip is " + ip);
        }
        if(sb.length() > 0){
        	log.debug(sb.toString());
        }
        return ip;

    }
	
}
